package org.csu.jpetstore_front.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.jpetstore_front.common.CommonResponse;
import org.csu.jpetstore_front.entity.*;
import org.csu.jpetstore_front.common.ResponseCode;
import org.csu.jpetstore_front.entity.Category;
import org.csu.jpetstore_front.entity.Inventory;
import org.csu.jpetstore_front.entity.Item;
import org.csu.jpetstore_front.entity.Product;
import org.csu.jpetstore_front.persistence.CategoryMapper;
import org.csu.jpetstore_front.persistence.InventoryMapper;
import org.csu.jpetstore_front.persistence.ItemMapper;
import org.csu.jpetstore_front.persistence.ProductMapper;
import org.csu.jpetstore_front.service.CatalogService;
import org.csu.jpetstore_front.vo.ItemVO;
import org.csu.jpetstore_front.vo.ProductVO;
import org.csu.jpetstore_front.vo.SproductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("catalogService")
public class CatalogServiceImpl implements CatalogService {

    private List spList;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private InventoryMapper inventoryMapper;

    @Override
    public CommonResponse<List<Category>> getCategoryList() {
        List<Category> categoryList = categoryMapper.selectList(null);
        if (categoryList.isEmpty()) {
            return CommonResponse.createForSuccessMessage("没有分类信息");
        }
        return CommonResponse.createForSuccess(categoryList);
    }

    @Override
    public CommonResponse<Category> getCategory(String categoryId) {
        Category category = categoryMapper.selectById(categoryId);
        if (category == null) {
            return CommonResponse.createForErrorMessage("没有该ID的Category");
        }
        return CommonResponse.createForSuccess(category);
    }

    @Override
    public CommonResponse<Product> getProduct(String productId) {
        Product product = productMapper.selectById(productId);
        if (product == null) {
            return CommonResponse.createForErrorMessage("没有该类");
        }
        return CommonResponse.createForSuccess(product);
    }

    @Override
    public CommonResponse<List<Product>> getProductListByCategory(String categoryId) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category", categoryId);
        List<Product> productList = productMapper.selectList(queryWrapper);
        if (productList.isEmpty()) {
            return CommonResponse.createForErrorMessage("没有1");

        }
        return CommonResponse.createForSuccess(productList);
    }

    @Override
    public CommonResponse<List<Product>> searchProductList(String keyword) {
        List<Product> productList = productMapper.selectList(null);
        if (productList.isEmpty()) {
            return CommonResponse.createForErrorMessage("没有2");

        }
        return CommonResponse.createForSuccess(productList);
    }

    @Override
    public CommonResponse<List<ItemVO>> getItemListByProduct(String productId) {
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid", productId);

        List<Item> itemList = itemMapper.selectList(queryWrapper);
        if (itemList.isEmpty()) {
            return CommonResponse.createForErrorMessage("该product下没有item商品");
        }
        Product product = productMapper.selectById(productId);
        List<ItemVO> itemVOList = new ArrayList<>();
        for (Item item : itemList) {
            ItemVO itemVO = itemToItemVO(item, product);
            itemVOList.add(itemVO);
        }
        return CommonResponse.createForSuccess(itemVOList);
    }

    private ItemVO itemToItemVO(Item item, Product product) {
        ItemVO itemVO = new ItemVO();
        itemVO.setItemId(item.getItemId());
        itemVO.setProductId(item.getProductId());
        itemVO.setListPrice(item.getListPrice());
        itemVO.setUnitCost(item.getUnitCost());
        itemVO.setSupplierId(item.getSupplierId());
        itemVO.setStatus(item.getStatus());
        itemVO.setAttribute1(item.getAttribute1());
        itemVO.setAttribute2(item.getAttribute2());
        itemVO.setAttribute3(item.getAttribute3());
        itemVO.setAttribute4(item.getAttribute4());
        itemVO.setAttribute5(item.getAttribute5());

        itemVO.setCategoryId(product.getCategoryId());
        itemVO.setProductName(product.getName());
        itemVO.setProductDescription(product.getDescription());

        Inventory inventory = inventoryMapper.selectById(item.getItemId());
        itemVO.setQuantiy(inventory.getQuantity());

        return itemVO;
    }

    @Override
    public CommonResponse<ItemVO> getItem(String itemId) {
        Item item = itemMapper.selectById(itemId);
        Product product = productMapper.selectById(item.getProductId());
        ItemVO itemVO = itemToItemVO(item, product);
        if (itemVO == null) {
            return CommonResponse.createForErrorMessage("没有4");
        }
        return CommonResponse.createForSuccess(itemVO);
    }


    @Override
    public CommonResponse<Boolean> isItemInStock(String itemId) {
        Item item = itemMapper.selectById(itemId);
        if (item == null) {
            return CommonResponse.createForErrorMessage("没有5");
        }
        return CommonResponse.createForSuccess(item.equals(1));
    }

    @Override
    public CommonResponse<List<ItemVO>> getAllItems() {
//        QueryWrapper<Item> queryWrapper=new QueryWrapper<>();
//        queryWrapper.eq("*",*);
        List<Item> itemList = itemMapper.selectList((QueryWrapper<ItemVO>) null);
        if (itemList.isEmpty()) {
            return CommonResponse.createForErrorMessage("该product下没有item商品");
        }

        List<ItemVO> itemVOList = new ArrayList<>();
        for (Item item : itemList) {
            Product product = productMapper.selectById(item.getProductId());
            ItemVO itemVO = itemToItemVO(item, product);
            itemVOList.add(itemVO);
        }
        return CommonResponse.createForSuccess(itemVOList);
    }

    @Override
    public CommonResponse<List<SproductVO>> getproduct_item() {

        List<Category> categoryList = categoryMapper.selectList(null);
        List<SproductVO> sproductVOS = new ArrayList<SproductVO>();

        categoryList.forEach(category -> {
            List<Product> productList = productMapper.selectList(new QueryWrapper<Product>().eq("category", category.getCategoryId()));
            List<ProductVO> productVOS = new ArrayList<>();

            SproductVO sproductVO = new SproductVO();
            productList.forEach(product -> {

                ProductVO productVO = new ProductVO();
                productVO.setProductId(product.getProductId());
                System.out.println(productVO.getProductId());
                productVO.setValue(product.getName());
                productVOS.add(productVO);
            });

            sproductVO.setCategoryId(category.getCategoryId());
            sproductVO.setProducts(productVOS);
            sproductVOS.add(sproductVO);
        });

        return CommonResponse.createForSuccess(sproductVOS);


    }
    @Override
    public CommonResponse<List<ItemVO>> getItemsByCategoryId(String categoryId) {
        List<ItemVO> itemVOList = new ArrayList<ItemVO>();
        List<Product> productList = productMapper.selectList(new QueryWrapper<Product>().eq("category",categoryId));
        productList.forEach(product -> {
            List<Item> itemList = itemMapper.selectList(new QueryWrapper<Item>().eq("productid",product.getProductId()));
            itemList.forEach(item -> {
                ItemVO itemVO = itemToItemVO(item,product);
                itemVOList.add(itemVO);
            });

        });
        return CommonResponse.createForSuccess(itemVOList);
    }

}
